package com.itheima.resolver;

import com.itheima.exception.ExceptionDemo;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExceptionResolver implements HandlerExceptionResolver {

    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        if (e instanceof ExceptionDemo.MyException) {
            modelAndView.addObject("msg", "自定义异常");

        } else if (e instanceof NumberFormatException) {
            modelAndView.addObject("msg", "数字转换异常");

        }
        modelAndView.setViewName("error.jsp");
        return modelAndView;
    }
}
